winbrew_cli\services\bootstrap/mod.rs
1//! Process bootstrap helpers for the CLI.
2//!
3//! This module owns the side effects that must happen before command execution
4//! can safely proceed. The goal is not to implement business logic here, but to
5//! make the process contract explicit:
6//!
7//! - install Ctrl+C handling early so cancellation is consistent across the
8//! whole process;
9//! - recover any interrupted installations before a command touches the same
10//! database rows or filesystem paths;
11//! - keep the code that mutates process-global state separate from the command
12//! dispatcher so startup remains easy to audit.
13//!
14//! The public entry point, [`init_runtime`], performs those bootstrap-only
15//! actions in the order required by the rest of the CLI. Logging setup lives in
16//! [`logging`] because it is a one-time process concern, while stale-install
17//! recovery lives in [`cleanup`] because it is a startup-only repair step.
18
19use anyhow::Result;
20
21use crate::cancel;
22
23pub mod cleanup;
24pub mod logging;
25
26/// Initialize runtime services that are required before any command runs.
27/// The sequence is intentionally minimal but strict. First the Ctrl+C handler
28/// is installed so command work can be interrupted safely. Then startup-only
29/// cleanup runs so any package rows left in the `Installing` state from a prior
30/// crash are marked failed and their leftover filesystem artifacts are removed.
31///
32/// This function is called after the database pool has been initialized and
33/// after logging has already been set up by the caller, so failures can be
34/// reported and the cleanup step can query the current install state.
35pub fn init_runtime() -> Result<()> {
36 cancel::init_handler()?;
37 cleanup::cleanup_stale_installations()?;
38
39 Ok(())
40}